package com.zat.mes.andrmes.dofun.work

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.contrarywind.view.WheelView
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.GetPackBean
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.data.MacCheckBean
import com.zat.mes.andrmes.data.ParantTypeItem
import com.zat.mes.andrmes.dofun.QrScanActivity
import com.zat.mes.andrmes.dofun.qualitytest.vm.QcPlanViewModel
import com.zat.mes.andrmes.dofun.work.adapter.GetPackAdapter
import com.zat.mes.andrmes.dofun.work.adapter.MacCheckListAdapter
import com.zat.mes.andrmes.dofun.work.vm.GetPackModel
import com.zat.mes.andrmes.dofun.work.vm.MachineModel
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.DateUtil
import com.zat.mes.andrmes.utils.DisplayUtil
import com.zat.mes.andrmes.utils.PreferencesUtil
import com.zat.mes.andrmes.utils.SystemActivityUtil
import kotlinx.android.synthetic.main.activity_mac_check.*
import kotlinx.android.synthetic.main.fragment_get_pack.*
import kotlinx.android.synthetic.main.layout_title.*
import kotlinx.android.synthetic.main.layout_title_right_date.*
import permissions.dispatcher.*
import java.util.*

/**
 * 机器检查历史列表
 */
class MacCheckActivity : BaseActivity() {
    var currentPage: Int = 1
    private var empId: Long = 0
    private var timePicker: TimePickerView? = null
    private var selectedDate: String = DateUtil.formatDate(Date(), DateUtil.FORMAT_3)
    private var macCheckModel: MachineModel? = null
    override fun init() {
        initView()
        initData()
    }

    private fun initView() {
        setContentView(R.layout.activity_mac_check)

        tVtitle.text = "机器运作检查"
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }
        val inflate = LayoutInflater.from(this).inflate(R.layout.layout_title_right_date, null)
        rlRightComm.addView(inflate)
        rlRightComm.visibility = View.VISIBLE
        val layoutParams: RelativeLayout.LayoutParams =
            inflate.layoutParams as RelativeLayout.LayoutParams
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE)
        layoutParams.rightMargin = DisplayUtil.dip2px(this, 30f)
        rlRightComm.setOnClickListener {
            timePicker?.show()
        }
        tvTimeTitleRight.text = selectedDate

    }

    private fun initData() {
        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        empId = loginResultBean.empid
        initDatePick()
        rvMacCheck.layoutManager = LinearLayoutManager(this)
        val mAdapter = MacCheckListAdapter(R.layout.item_mac_check_list)
        rvMacCheck.adapter = mAdapter

        val loadMoreModule = mAdapter.loadMoreModule
        // 当数据不满一页时，是否继续自动加载（默认为true）
        loadMoreModule?.isEnableLoadMoreIfNotFullPage = false
        loadMoreModule?.isAutoLoadMore = false
        loadMoreModule?.setOnLoadMoreListener(OnLoadMoreListener {
            requestData()
        })

        macCheckModel = ViewModelProvider(this).get(MachineModel::class.java)

        //获取列表
        macCheckModel?.getMachineCheckListData()?.observe(this, androidx.lifecycle.Observer {
            closeLoading()
            if (it != null) {
                val result = it.result
                if (result != null) {
                    if (result.items != null) {
                        if (currentPage == 1) {
                            if (result.items == null || result.items.size <= 0) {
                                setEmptyVisible()
                            } else {
                                setEmptyGone()
                            }
                            mAdapter.setNewData(result.items as MutableList<MacCheckBean>)
                        } else {
                            mAdapter.addData(result.items)
                        }
                    }
                    if (result.currentPage == result.totalPage) {
                        loadMoreModule?.loadMoreEnd()
                    } else {
                        loadMoreModule?.loadMoreComplete()
                        currentPage++
                    }
                } else {
                    loadMoreModule?.loadMoreEnd()
                }
            } else {
                loadMoreModule?.loadMoreEnd()
            }

        })

        requestData()
    }

    /**
     * 初始化时间选择器
     */
    private fun initDatePick() {
        val booleanArray = booleanArrayOf(true, true, true, false, false, false)
        timePicker = TimePickerBuilder(this, object : OnTimeSelectListener {
            override fun onTimeSelect(date: Date?, v: View?) {
                if (date != null) {
                    selectedDate = DateUtil.formatDate(date, DateUtil.FORMAT_3)
                    tvTimeTitleRight.text = DateUtil.formatDate(date, DateUtil.FORMAT_3)
                    currentPage=1
                    requestData()

                }
            }

        }).setSubmitColor(ContextCompat.getColor(this, R.color.color_text_nomal))
            .setCancelColor(ContextCompat.getColor(this, R.color.color_text_nomal))
            .setTitleBgColor(ContextCompat.getColor(this, R.color.color_white))
            .setContentTextSize(18)
            .setType(booleanArray)
            .setLabel("", "", "", "", "", "")
            .setDividerType(WheelView.DividerType.WRAP)
            .setDividerColor(ContextCompat.getColor(this, R.color.color_theme))
            .setLineSpacingMultiplier(2.0f)
            .build()
    }

    private fun requestData() {
        showLoading()
        macCheckModel?.getMachineCheckList(
            currentPage,
            20,
            empId.toString(),
            selectedDate
        )

    }


}